//
//  HomeViewController.swift
//  weibo
//
//  Created by wang_h on 2017/5/24.
//  Copyright © 2017年 wang_h. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {

    //MARK:- 懒加载
    lazy var titleBtn : TitleButton = TitleButton()
    //注意：在闭包中如果使用当前对象属性或者调用方法也需要加self
    //两个地方需要使用self : 1>如果在一个函数中出现歧义 2>在闭包中使用当前对象的属性和方法也需要加self
    lazy var popoverAnimator : PopoverAnimator = PopoverAnimator { [weak self] (presented) in
        self!.titleBtn.isSelected = presented
    }
    
    lazy var statuses : [Status] = [Status]()
    
    //MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.没有登录时设置的内容
        visitorView.addRotationAnim()
        if !isLogin {
            return
        }
        //2.设置导航栏的方法
        setupNavigationBar()
        
        //3.请求数据
        loadStatuses()
    }
}
//MARK:- 设置UI界面
extension HomeViewController {
    func setupNavigationBar() {
        //1.设置左侧的Item
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention")
        //2.设置右侧的Item
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        //3.设置titleView
        titleBtn.setTitle("coderwhy", for: .normal)
        titleBtn.addTarget(self, action: #selector(titleBtnClickTitleBtn), for: .touchUpInside)
        navigationItem.titleView = titleBtn
    }
}

//MARK:- 事件监听函数
extension HomeViewController {
    func titleBtnClickTitleBtn() {
        //1.改变按钮的状态
        titleBtn.isSelected = !titleBtn.isSelected
        //2.创建弹出的控制器
        let popoverVC = PopoverViewController()
        
        //3.设置控制器的modal样式
        popoverVC.modalPresentationStyle = .custom
        popoverVC.transitioningDelegate = popoverAnimator
        popoverAnimator.presentedFrame = CGRect(x: kScreenW/2 - 90, y: 55, width: 180, height: 250)

        //4.弹出控制器
        present(popoverVC, animated: true, completion: nil)
    }
}

//MARK:- 请求数据
extension HomeViewController {
    
    func loadStatuses() {
        NetWorkTools.shareInstance.loadStatuses { (result, error) in
            //错误校验
            if error != nil { return }
            
            //获取可以选类型中的数据
            guard let resultArray = result else { return }
            
            //遍历微博对应的字典
            for statusDict in resultArray {
                let status = Status(dict: statusDict)
                self.statuses.append(status)
            }
            
            //刷新表格
            self.tableView.reloadData()
        }
    }
}

extension HomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCellID")!
        
        let state = statuses[indexPath.row]
        
        cell.textLabel?.text = state.text
        
        return cell
    }
}






